package pers.whj.net.local.task;

public class Task {
    public static final int DEFAULT = 1;
    public static final int MIN_PRIORITY = 0;
    public static final int MAX_PRIORITY = 9;

    protected float mCurrentSpeed;
    protected int mUpdateSpeedInterval;

    private String mName;
    private int mPriority;
    private int mType;
    private final long mCreateTime;
    private long mStartTime;
    private long mEndTime;
    private boolean mIsSuccess;
    private Exception mException;
    private byte[] mData;
    private boolean mIsRunning;

    public Task() {
        this(DEFAULT);
    }

    public Task(int type) {
        this(type, "");
    }

    public Task(int type, String name) {
        this(type, name, 1);
    }

    public Task(int type, String name, int priority) {
        mName = name;
        mPriority = priority;
        mType = type;
        mCreateTime = System.currentTimeMillis();
        init();
    }

    public void init() {
        mCurrentSpeed = 0f;
        mUpdateSpeedInterval = 100;
        mIsSuccess = true;
        mIsRunning = false;
    }

    public String getCurrentProgress() {
        return "";
    }

    public String getCurrentSpeed() {
        if (!mIsRunning) {
            mCurrentSpeed = 0f;
        }
        return formatSpeed(mCurrentSpeed);
    }

    public String getAverageSpeed() {
        return formatSpeed(0f);
    }

    public String formatSpeed(float speed) {
        String unit = "B/s";
        String speedStr;
        if (speed > 0) {
            if (speed >= 1024) {
                speed /= 1024;
                unit = "KB/s";
            }
            if (speed >= 1024) {
                speed /= 1024;
                unit = "MB/s";
            }
            speedStr = String.format("%.2f", speed);
        } else {
            speedStr = "0";
        }
        return speedStr + unit;
    }

    public void setUpdateSpeedInterval(int interval) {
        if (interval > 0) {
            mUpdateSpeedInterval = interval;
        }
    }

    public int getUpdateSpeedInterval() {
        return mUpdateSpeedInterval;
    }

    public String getName() {
        return mName;
    }

    public void setName(String name) {
        if (name != null) {
            mName = name;
        }
    }

    public int getPriority() {
        return mPriority;
    }

    public void setPriority(int priority) {
        priority = Math.min(priority, MAX_PRIORITY);
        priority = Math.max(priority, MIN_PRIORITY);
        mPriority = priority;
    }

    public int getType() {
        return mType;
    }

    public void setType(int type) {
        mType = type;
    }

    public long getCreateTime() {
        return mCreateTime;
    }

    public void setStartTime(long startTime) {
        mStartTime = startTime;
        mIsRunning = true;
    }

    public long getStartTime() {
        return mStartTime;
    }

    public void setEndTime(long endTime) {
        mEndTime = endTime;
        mIsRunning = false;
    }

    public long getEndTime() {
        return mEndTime;
    }

    public void setSuccess(boolean success) {
        mIsSuccess = success;
        mIsRunning = false;
    }

    public boolean isSuccess() {
        return mIsSuccess;
    }

    public boolean isRunning() {
        return mIsRunning;
    }

    public Exception getException() {
        return mException;
    }

    public void setException(Exception exception) {
        mException = exception;
    }

    public void setData(byte[] data) {
        mData = data;
    }

    public byte[] getData() {
        return mData;
    }
}